﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Reflection;

namespace BizElements.Core
{
    /// <summary>Provivides common operations ADO.NET objects: DataTable, DataRow etc.</summary>
    public static class AdoNetUtil
    {
        #region DataRow.

        /// <summary>Tries to gets value stored in the specified column. <see cref="DBNull"/> is <b>NOT</b> converted.</summary>
        /// <param name="row">Row.</param>
        /// <param name="columnName">Column name. If not found then <see cref="DBNull"/> will be returned.</param>
        /// <returns>Value stored in the column or <see cref="DBNull"/>.</returns>
        public static object TryGetColumnValue(DataRow row, string columnName)
        {            
            int colIdx = row.Table.Columns.IndexOf(columnName);
            return (colIdx >= 0) ? row[colIdx] : DBNull.Value;
        }

        /// <summary>Gets value stored in the specified column. <see cref="DBNull"/> is <b>NOT</b> converted.</summary>
        /// <param name="row">Row.</param>
        /// <param name="column">Column. If not fount then an exceptio is generated.</param>
        /// <returns>Value stored in the column.</returns>
        public static object GetColumnValue(DataRow row, IDbColumn column)
        {
            int colIdx = row.Table.Columns.IndexOf(column.Alias);
            if (colIdx < 0)
                colIdx = row.Table.Columns.IndexOf(column.ColumnName);
            if (colIdx < 0)
                throw new ArgumentException("DataTable doesn't contain the specified column (" + column.ColumnName + ").");

            return row[colIdx];
        }

        #endregion

        #region DataTable.

        /// <summary>Sorts DataTable.</summary>
        /// <param name="data">Data to sort.</param>
        /// <param name="column">Column name by which the data will be sorted.</param>
        /// <returns>Sorted data.</returns>
        public static DataTable SortDataTable(DataTable data, string column)
        {
            DataTable sortedTable = data.Clone();
            DataRow[] sortedRows = data.Select(null, column);
            foreach (DataRow row in sortedRows)
                sortedTable.ImportRow(row);

            return sortedTable;
        }

        /// <summary>Sorts DataTable.</summary>
        /// <param name="data">Data to sort.</param>
        /// <param name="column">Column name by which the data will be sorted.</param>
        /// <param name="ascending">Specifies whether the values in the specified column should be sorted in ascendin order, from lowest value to highest value.</param>
        /// <returns>Sorted data.</returns>
        public static DataTable SortDataTable(DataTable data, string column, bool ascending)
        {
            string sort = new OrderByClause(column, ascending).GetDataViewSortExpression();
            DataTable sortedTable = data.Clone();
            DataRow[] sortedRows = data.Select(null, sort);
            foreach (DataRow row in sortedRows)
                sortedTable.ImportRow(row);

            return sortedTable;
        }

        #endregion
    }
}
